<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>类型运算符</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.operators.array.html">« 数组运算符</a></li>
      <li style="float: right;"><a href="language.control-structures.html">流程控制 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.operators.html">运算符</a></li>
    <li>类型运算符</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.operators.type" class="sect1">
   <h2 class="title">类型运算符</h2>
   <p class="para">
    <code class="literal">instanceof</code> 用于确定一个 PHP 变量是否属于某一类
    <a href="language.oop5.basic.html#language.oop5.basic.class" class="link">class</a> 的实例：
    <div class="example" id="example-122">
     <p><strong>示例 #1 对类使用 <code class="literal">instanceof</code></strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass<br /></span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">NotMyClass<br /></span><span style="color: #007700">{<br />}<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">NotMyClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(false)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    <code class="literal">instanceof</code>　也可用来确定一个变量是不是继承自某一父类的子类的实例：
    <div class="example" id="example-123">
     <p><strong>示例 #2 对继承类使用 <code class="literal">instanceof</code></strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ParentClass<br /></span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">ParentClass<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">ParentClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(true)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    检查一个对象是否<em class="emphasis">不是</em>某个类的实例，可以使用<a href="language.operators.logical.html" class="link">逻辑运算符 <code class="literal">not</code></a>。
    <div class="example" id="example-124">
     <p><strong>示例 #3 使用 <code class="literal">instanceof</code> 检查对象<em class="emphasis">不是</em>某个类的实例</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(!(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    最后，<code class="literal">instanceof</code>也可用于确定一个变量是不是实现了某个<a href="language.oop5.interfaces.html" class="link">接口</a>的对象的实例:
    <div class="example" id="example-125">
     <p><strong>示例 #4 对接口使用 <code class="literal">instanceof</code></strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">MyInterface</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(true)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    虽然 <code class="literal">instanceof</code> 通常直接与类名一起使用，但也可以使用对象或字符串变量：
    <div class="example" id="example-126">
     <p><strong>示例 #5 对其它变量使用 <code class="literal">instanceof</code></strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">MyClass&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">MyInterface<br /></span><span style="color: #007700">{<br />}<br /><br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'NotMyClass'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;是&nbsp;MyClass&nbsp;类的对象<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$c&nbsp;是字符串&nbsp;'MyClass'<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$d&nbsp;是字符串&nbsp;'NotMyClass'<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(true)
bool(false)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    如果被检测的变量不是对象，instanceof 并不发出任何错误信息而是返回
    <strong><code>false</code></strong>。PHP 7.3.0 之前不允许用于检测常量。
    <div class="example" id="example-127">
     <p><strong>示例 #6 用 <code class="literal">instanceof</code> 检测其它变量</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreate</span><span style="color: #007700">(</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$a&nbsp;是整数<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$b&nbsp;是&nbsp;NULL<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$c&nbsp;是资源<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(false)
bool(false)
PHP Fatal error:  instanceof expects an object instance, constant given
</pre></div>
     </div>
    </div>
   </p><p class="para">
    PHP 7.3.0 起，
    <code class="literal">instanceof</code> 操作符的左侧可以放常量。
    <div class="example" id="example-128">
     <p><strong>示例 #7 使用 <code class="literal">instanceof</code> 测试常量</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">FALSE&nbsp;</span><span style="color: #007700">instanceof&nbsp;</span><span style="color: #0000BB">stdClass</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程在 PHP 7.3 中的输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    PHP 8.0.0 起， <code class="literal">instanceof</code> 可以与任何表达式一起使用。表达式必须使用括号括起来并且生成
    <span class="type">string</span>。
    
    <div class="example" id="example-129">
     <p><strong>示例 #8 将 <code class="literal">instanceof</code> 与任意表达式一起使用</strong></p>
     <div class="example-contents">
      <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ClassA&nbsp;</span><span style="color: #007700">extends&nbsp;\</span><span style="color: #0000BB">stdClass&nbsp;</span><span style="color: #007700">{}<br />class&nbsp;</span><span style="color: #0000BB">ClassB&nbsp;</span><span style="color: #007700">extends&nbsp;\</span><span style="color: #0000BB">stdClass&nbsp;</span><span style="color: #007700">{}<br />class&nbsp;</span><span style="color: #0000BB">ClassC&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">ClassB&nbsp;</span><span style="color: #007700">{}<br />class&nbsp;</span><span style="color: #0000BB">ClassD&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">ClassA&nbsp;</span><span style="color: #007700">{}<br /><br />function&nbsp;</span><span style="color: #0000BB">getSomeClass</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">string<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">ClassA</span><span style="color: #007700">::class;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ClassA&nbsp;</span><span style="color: #007700">instanceof&nbsp;(</span><span style="color: #DD0000">'std'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'Class'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ClassB&nbsp;</span><span style="color: #007700">instanceof&nbsp;(</span><span style="color: #DD0000">'Class'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'B'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ClassC&nbsp;</span><span style="color: #007700">instanceof&nbsp;(</span><span style="color: #DD0000">'Class'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ClassD&nbsp;</span><span style="color: #007700">instanceof&nbsp;(</span><span style="color: #0000BB">getSomeClass</span><span style="color: #007700">()));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程在 PHP 8 中的输出：</p></div>
     <div class="example-contents screen">
      <div class="cdata"><pre>
bool(true)
bool(true)
bool(false)
bool(true)
</pre></div>
     </div>
    </div>
   </p>
   <p class="simpara">
    <code class="literal">instanceof</code> 在功能上有个
    类似的变体 <span class="function"><a href="function.is-a.html" class="function">is_a()</a></span>。
   </p>

   <div class="sect2">
    <h3 class="title">参见</h3>
    <p class="para">
     <ul class="simplelist">
      <li class="member"><span class="function"><a href="function.get-class.html" class="function">get_class()</a></span></li>
      <li class="member"><span class="function"><a href="function.is-a.html" class="function">is_a()</a></span></li>
     </ul>
    </p>
   </div>
  </div></div></div></body></html>